Step 1: The Skeleton & peek()

Let's start our PriorityQueue class.

Guidance for Step 1

  • Docstring: Fill in the formulas for Parent, Left Child, and Right Child.
  • __init__: All we need is an empty list to hold the heap data.
  • peek(): This is the easiest operation. The Max-Heap Property guarantees the largest item is at the root.
  • Your Task: What is the array index for the root?
class PriorityQueue:
    """
    Implements a Max-Priority Queue (0-based indexing).
    Parent/child relationships for an element at index 'i':
     - Parent: ________
     - Left Child: ________
     - Right Child: ________
    """

    def __init__(self):
        """Initializes an empty list to store the heap elements."""
        self.heap = []

    def is_empty(self):
        """Helper method to check if the heap is empty."""
        return len(self.heap) == 0

    def peek(self):
        """
        Returns the maximum key without removing it.
        Returns "null" if the heap is empty.
        """
        if self.is_empty():
            return "null"
        
        # The max element is always at the root in a max-heap
        return self.heap[____]

                
Copied!